package ucsd.ties.dvs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class CreatePanel {
	@SuppressWarnings("unused")
	private WritableCellFormat arial;
	private static String fullname;
	private static String textFile;

	// Text fields
	private JTextField receiptNumField;
	private JTextField firstNameField;
	private JTextField lastNameField;
	private JTextField dateField;
	private JTextField locationField;
	private JTextField screenerField;
	private JTextField birthdayField;
	private JTextField ageField;
	private JTextField genderField;
	private JTextField ethnicityField;
	private JTextField roomField;
	private JTextField commentField;
	private JTextField schoolYearField;
	private JTextField languageField;
	private JTextField regionField;
	private JTextField zipField;
	private JTextField schoolIDField;
	private JTextField organizationField;
	private JTextField locField;
	private JTextField replaceField;

	// Buttons
	private JButton finishedButton;
	private JButton clearButton;
	private JButton nextButton;
	private JButton deleteButton;
	@SuppressWarnings("unused")
	private JButton replaceButton;
	private JButton openButton;
	private JButton openTextButton;
	private JButton saveButton;
	private JRadioButton leftButton;
	private JRadioButton rightButton;

	private JFileChooser fc;

	private static JFrame frame = new JFrame("Autorefractor Data Input System");
	private static JFrame inputFrame1 = new JFrame(
			"Autorefractor Data Input System");
	private static JFrame opentextframe = new JFrame(
			"Autorefractor Data Input System");
	private static JComponent inputPanel;

	// private ValueHolder reportNameVH;
	private static boolean isOpenFile = false;
	private static boolean isCreateNewFile = false;
	private static String gender;
	private static String eth;
	private static String language;
	private static String comment;

	public static void main(String[] args) {
		showWelcomPageGUI();
	}

	private static void showInputInfoGUI() {
		inputFrame1.setTitle("Autorefractor Data Input System");
		inputFrame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		inputPanel = new CreatePanel().buildPanel();
		inputPanel.setPreferredSize(new Dimension(500, 700));
		inputFrame1.getContentPane().add(inputPanel);
		inputFrame1.pack();
		inputPanel.setBorder(BorderFactory
				.createTitledBorder("Please Enter All Necessary Information"));
		inputFrame1.setResizable(false);
		inputFrame1.setVisible(true);
	}

	private static void showWelcomPageGUI() {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JComponent panel = new CreatePanel().saveOpenFilePanel();
		panel.setPreferredSize(new Dimension(400, 400));
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setBorder(BorderFactory
				.createTitledBorder("Please Choose the Necessary Option"));
		frame.pack();
		frame.setResizable(false);
		frame.setVisible(true);
	}

	private static void showOpenTextGUI() {
		opentextframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JComponent panel = new CreatePanel().openTextFilePanel();
		panel.setPreferredSize(new Dimension(400, 400));
		opentextframe.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setBorder(BorderFactory
				.createTitledBorder("Please Choose the Necessary Text File "));
		opentextframe.pack();
		opentextframe.setResizable(false);
		opentextframe.setVisible(true);
	}

	private void initComponents() {
		receiptNumField = new JTextField();
		firstNameField = new JTextField();
		lastNameField = new JTextField();
		dateField = new JTextField();
		locationField = new JTextField();
		screenerField = new JTextField();
		birthdayField = new JTextField();
		regionField = new JTextField();
		zipField = new JTextField();
		schoolIDField = new JTextField();
		organizationField = new JTextField();
		locField = new JTextField();
		ageField = new JTextField();
		genderField = new JTextField();
		ethnicityField = new JTextField();
		roomField = new JTextField();
		commentField = new JTextField();
		schoolYearField = new JTextField();
		languageField = new JTextField();
		replaceField = new JTextField();
		// Set replaceField to false
		replaceField.setEditable(false);

		finishedButton = new JButton(new finishAction());
		nextButton = new JButton(new NextAction());
		clearButton = new JButton(new cancelAction());
		saveButton = new JButton(new CreateNewFileAction());
		openButton = new JButton(new OpenAction());
		openTextButton = new JButton(new openTextAction());
		deleteButton = new JButton(new deleteAction());
		// replaceButton = new JButton(new replaceAction());
		leftButton = new JRadioButton("Left Eye", true);
		rightButton = new JRadioButton("Right Eye", false);

		fc = new JFileChooser();
	}

	public JComponent saveOpenFilePanel() {
		initComponents();
		JPanel buttonPanel = new JPanel();
		openButton.setPreferredSize(new Dimension(400, 100));
		saveButton.setPreferredSize(new Dimension(400, 100));
		buttonPanel.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
		buttonPanel.add(openButton);
		buttonPanel.add(saveButton);
		return buttonPanel;
	}

	public JComponent openTextFilePanel() {
		initComponents();
		JPanel buttonPanel = new JPanel();
		openTextButton.setPreferredSize(new Dimension(400, 100));
		buttonPanel.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
		buttonPanel.add(openTextButton);
		return buttonPanel;
	}

	public JComponent buildPanel() {
		initComponents();
		FormLayout layout = new FormLayout(
				"25dlu, 50dlu, 100dlu, 20dlu, 40dlu, 50dlu, pref",
				"p, p, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, "
						+ "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, "
						+ "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu");
		JPanel mainPanel = new JPanel(layout);
		mainPanel.setBorder(Borders.DIALOG_BORDER);
		CellConstraints cc = new CellConstraints();
		mainPanel.add(new JLabel("Receipt No. "), cc.xy(2, 3));
		mainPanel.add(receiptNumField, cc.xy(3, 3));
		mainPanel.add(new JLabel("First Name"), cc.xy(2, 5));
		mainPanel.add(firstNameField, cc.xy(3, 5));
		mainPanel.add(new JLabel("Last Name"), cc.xy(2, 7));
		mainPanel.add(lastNameField, cc.xy(3, 7));
		mainPanel.add(new JLabel("Date of Birth"), cc.xy(2, 9));
		mainPanel.add(birthdayField, cc.xy(3, 9));
		mainPanel.add(new JLabel("Gender"), cc.xy(2, 11));
		mainPanel.add(genderField, cc.xy(3, 11));
		mainPanel.add(new JLabel("Age"), cc.xy(2, 13));
		mainPanel.add(ageField, cc.xy(3, 13));
		mainPanel.add(new JLabel("Ethnicity"), cc.xy(2, 15));
		mainPanel.add(ethnicityField, cc.xy(3, 15));
		mainPanel.add(new JLabel("Screener"), cc.xy(2, 17));
		mainPanel.add(screenerField, cc.xy(3, 17));
		mainPanel.add(new JLabel("Date"), cc.xy(2, 19));
		mainPanel.add(dateField, cc.xy(3, 19));
		mainPanel.add(new JLabel("Location"), cc.xy(2, 21));
		mainPanel.add(locationField, cc.xy(3, 21));
		mainPanel.add(new JLabel("Room"), cc.xy(2, 23));
		mainPanel.add(roomField, cc.xy(3, 23));
		mainPanel.add(new JLabel("Region"), cc.xy(2, 25));
		mainPanel.add(regionField, cc.xy(3, 25));
		mainPanel.add(new JLabel("Zip Code"), cc.xy(2, 27));
		mainPanel.add(zipField, cc.xy(3, 27));
		mainPanel.add(new JLabel("School ID"), cc.xy(2, 29));
		mainPanel.add(schoolIDField, cc.xy(3, 29));
		mainPanel.add(new JLabel("School Year"), cc.xy(2, 31));
		mainPanel.add(schoolYearField, cc.xy(3, 31));
		mainPanel.add(new JLabel("Organization"), cc.xy(2, 33));
		mainPanel.add(organizationField, cc.xy(3, 33));
		mainPanel.add(new JLabel("Language"), cc.xy(2, 35));
		mainPanel.add(languageField, cc.xy(3, 35));
		mainPanel.add(new JLabel("Loc. No."), cc.xy(2, 37));
		mainPanel.add(locField, cc.xy(3, 37));
		mainPanel.add(new JLabel("Comment"), cc.xy(2, 39));
		mainPanel.add(commentField, cc.xy(3, 39));
		mainPanel.add(new JLabel("Replace With"), cc.xy(2, 41));
		mainPanel.add(replaceField, cc.xy(3, 41));

		ButtonGroup bgroup = new ButtonGroup();
		bgroup.add(leftButton);
		bgroup.add(rightButton);
		JPanel radioPanel = new JPanel();
		radioPanel.setLayout(new GridLayout(1, 2));
		radioPanel.add(leftButton);
		radioPanel.add(rightButton);
		radioPanel.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(), "Which Eye?"));
		mainPanel.add(radioPanel, cc.xyw(2, 43, 2));

		JPanel panelFirstRowButtons = new JPanel();
		panelFirstRowButtons.setLayout(new GridLayout(1, 2));
		clearButton.setPreferredSize(new Dimension(405, 15));
		panelFirstRowButtons.add(clearButton);
		nextButton.setPreferredSize(new Dimension(405, 10));
		panelFirstRowButtons.add(nextButton);

		JPanel panelSecondRowButtons = new JPanel();
		panelSecondRowButtons.setLayout(new GridLayout(1, 2));
		// replaceButton.setPreferredSize(new Dimension(405, 10));
		// panelSecondRowButtons.add(replaceButton);
		deleteButton.setPreferredSize(new Dimension(405, 15));
		panelSecondRowButtons.add(deleteButton);

		JPanel panelThirdRowButtons = new JPanel();
		panelThirdRowButtons.setLayout(new GridLayout(1, 1));
		finishedButton.setPreferredSize(new Dimension(810, 15));
		panelThirdRowButtons.add(finishedButton);

		JPanel panelButtons = new JPanel();
		panelButtons.setLayout(new GridLayout(3, 1));
		panelButtons.setPreferredSize(new Dimension(810, 130));
		panelButtons.add(panelFirstRowButtons);
		panelButtons.add(panelSecondRowButtons);
		panelButtons.add(panelThirdRowButtons);
		mainPanel.add(panelButtons, cc.xyw(2, 45, 2));

		// cc.xyw(2, 41, 2)
		// cc.xyw(2, 43, 2)
		// cc.xyw(2, 45, 2)
		// cc.xyw(2, 47, 2)
		return mainPanel;
	}

	private void setIsOpenFile(boolean b) {
		isOpenFile = b;
	}

	static boolean getIsOpenFile() {
		return isOpenFile;
	}

	static void setIsCreateNewFile(boolean b) {
		isCreateNewFile = b;
	}

	static boolean getIsCreateNewFile() {
		return isCreateNewFile;
	}

	private boolean checkIsDigit(String num) {
		for (int i = 0; i < num.length(); i++) {
			if (!Character.isDigit(num.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	private boolean checkIsCharacter(String s) {
		for (int i = 0; i < s.length(); i++) {
			if (!Character.isLetter(s.charAt(i))
					&& !Character.isSpaceChar(s.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	private boolean checkDate(String num) {
		boolean dateFlag = false;
		if (num.length() != 10) {
			dateFlag = true;
			return dateFlag;
		} else {
			for (int i = 0; i < num.length(); i++) {
				if (i == 0 || i == 1 || i == 3 || i == 4 || i == 6 || i == 7
						|| i == 8 || i == 9) {
					if (Character.isDigit(num.charAt(i))) {
						dateFlag = false;
					}
				} else if (i == 2 || i == 5) {
					if (num.substring(i).equals("-")) {
						dateFlag = false;
					}
				} else {
					dateFlag = true;
				}
			}
			return dateFlag;
		}
	}

	private boolean checkDayAndMonth(String num) {
		boolean dateFlag = false;
		int day, month;
		month = Integer.parseInt(num.substring(0, 2));
		day = Integer.parseInt(num.substring(3, 5));

		if (day > 31 || month > 12) {
			dateFlag = true;
		}
		return dateFlag;
	}

	private boolean checkGender(String s) {
		if (s.equalsIgnoreCase("M") || s.equalsIgnoreCase("Male")) {
			gender = "Male";
			return true;
		} else if (s.equalsIgnoreCase("F") || s.equalsIgnoreCase("Female")) {
			gender = "Female";
			return true;
		} else {
			return false;
		}
	}

	private boolean checkEthnicity(String s) {
		if (s.equalsIgnoreCase("A") || s.equalsIgnoreCase("Asian")) {
			eth = "Asian";
			return true;
		} else if (s.equalsIgnoreCase("C") || s.equalsIgnoreCase("Caucasion")) {
			eth = "Caucasion";
			return true;
		} else if (s.equalsIgnoreCase("AA")
				|| s.equalsIgnoreCase("African American")) {
			eth = "African American";
			return true;
		} else if (s.equalsIgnoreCase("H") || s.equalsIgnoreCase("Hispanic")) {
			eth = "Hispanic";
			return true;
		} else if (s.equalsIgnoreCase("EI")
				|| s.equalsIgnoreCase("East Indian")) {
			eth = "East Indian";
			return true;
		} else if (s.equalsIgnoreCase("ME")
				|| s.equalsIgnoreCase("Middle Eastern")) {
			eth = "Middle Eastern";
			return true;
		} else if (s.equalsIgnoreCase("NA")
				|| s.equalsIgnoreCase("Native American")) {
			eth = "Native American";
			return true;
		} else if (s.equalsIgnoreCase("PI")
				|| s.equalsIgnoreCase("Pacific Islander")) {
			eth = "Pacific Islander";
			return true;
		} else {
			return false;
		}
	}

	private boolean checkLanguage(String s) {
		if (s.equalsIgnoreCase("E") || s.equalsIgnoreCase("English")) {
			language = "English";
			return true;
		} else if (s.equalsIgnoreCase("S") || s.equalsIgnoreCase("Spanish")) {
			language = "Spanish";
			return true;
		} else if (s.equalsIgnoreCase("O") || s.equalsIgnoreCase("Other")) {
			language = "Other";
			return true;
		} else {
			return false;
		}
	}

	private boolean checkComment(String s) {
		if (s.equalsIgnoreCase("R") || s.equalsIgnoreCase("Refer")) {
			comment = "Refer";
			return true;
		} else {
			return false;
		}
	}

	private void resetErrorField(JTextField field) {
		field.setBackground(Color.WHITE);
		field.setToolTipText("");
	}

	@SuppressWarnings("serial")
	private final class openTextAction extends AbstractAction {
		private openTextAction() {
			super("Open Text File", new ImageIcon("openfile.png"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			int returnVal = fc.showOpenDialog(null);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				fc.setCurrentDirectory(new File("."));
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				File file = fc.getSelectedFile();
				textFile = file.getAbsolutePath();
				// JOptionPane.showMessageDialog(null, fullname);
				opentextframe.setVisible(false);
				showInputInfoGUI();
			}
		}
	}

	/*****************************************************
	 * Creating a new Excel File by reading the receipt
	 ******************************************************/
	@SuppressWarnings("serial")
	private final class CreateNewFileAction extends AbstractAction {
		private CreateNewFileAction() {
			super("Create New File", new ImageIcon("newfile.png"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			setIsCreateNewFile(true);
			setIsOpenFile(false);
			int returnVal = fc.showSaveDialog(null);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				fc.setCurrentDirectory(new File("."));
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				File file = fc.getSelectedFile();
				fullname = file.getAbsolutePath();
				frame.setVisible(false);
				showOpenTextGUI();
			}
		}
	}

	/***************************************************
	 * Open existing Excel File to write new Data
	 ****************************************************/
	@SuppressWarnings("serial")
	private final class OpenAction extends AbstractAction {
		private OpenAction() {
			super("Open Existing File", new ImageIcon("openfile.png"));

		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			setIsOpenFile(true);
			setIsCreateNewFile(false);
			int returnVal = fc.showOpenDialog(null);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				fc.setCurrentDirectory(new File("."));
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				File file = fc.getSelectedFile();
				fullname = file.getAbsolutePath();
				frame.setVisible(false);
				showInputInfoGUI();
			}
		}
	}

	/******************************************************
	 * Adding new data to new/existing Excel File
	 *****************************************************/
	@SuppressWarnings("serial")
	private final class NextAction extends AbstractAction {
		private NextAction() {
			super("Next", new ImageIcon("next.png"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			WritableFont arial10 = new WritableFont(WritableFont.ARIAL, 10);
			arial = new WritableCellFormat(arial10);

			int numberError = 0;

			if (receiptNumField.getText().length() == 0
					|| !checkIsDigit(receiptNumField.getText())) {
				numberError++;
				receiptNumField.setBackground(Color.PINK);
				receiptNumField
						.setToolTipText("Receipt number must contain only digits 0-9");
			} else {
				resetErrorField(receiptNumField);
			}

			if (firstNameField.getText().length() == 0
					|| !checkIsCharacter(firstNameField.getText())) {
				numberError++;
				firstNameField.setBackground(Color.PINK);
				firstNameField
						.setToolTipText("Patient's first name must contain only letter A-Z");
			} else {
				resetErrorField(firstNameField);
			}

			if (lastNameField.getText().length() == 0
					|| !checkIsCharacter(lastNameField.getText())) {
				numberError++;
				lastNameField.setBackground(Color.PINK);
				lastNameField
						.setToolTipText("Patient's last name must contain only letter A-Z");
			} else {
				resetErrorField(lastNameField);
			}

			if (dateField.getText().length() == 0
					|| checkDate(dateField.getText())) {
				numberError++;
				dateField.setBackground(Color.PINK);
				dateField.setToolTipText("Date must have a format mm-dd-yyyy");
			} else if (checkDayAndMonth(dateField.getText())) {
				numberError++;
				dateField.setBackground(Color.PINK);
				dateField
						.setToolTipText("Day and month cannot be greater than 31 and 12");
			} else {
				resetErrorField(dateField);
			}

			if (locationField.getText().length() == 0) {
				numberError++;
				locationField.setBackground(Color.PINK);
				locationField.setToolTipText("Field cannot be empty");
			} else {
				resetErrorField(locationField);
			}

			if (screenerField.getText().length() == 0
					|| !checkIsCharacter(screenerField.getText())) {
				numberError++;
				screenerField.setBackground(Color.PINK);
				screenerField
						.setToolTipText("Sceener's name must contain only letter A-Z");
			} else {
				resetErrorField(screenerField);
			}

			if (checkDate(birthdayField.getText())) {
				numberError++;
				birthdayField.setBackground(Color.PINK);
				birthdayField
						.setToolTipText("DOB must have a format mm-dd-yyyy");
			} else if (checkDayAndMonth(birthdayField.getText())) {
				numberError++;
				birthdayField.setBackground(Color.PINK);
				birthdayField
						.setToolTipText("Day and month cannot be greater than 31 and 12");
			} else {
				resetErrorField(birthdayField);
			}

			if (regionField.getText().length() == 0
					|| (!checkIsDigit(locField.getText()) && !checkIsCharacter(locField
							.getText()))) {
				numberError++;
				regionField.setBackground(Color.PINK);
				regionField
						.setToolTipText("Region Field must contain only letter A-Z");
			} else {
				resetErrorField(regionField);
			}
			if (zipField.getText().length() == 0) {
				numberError++;
				zipField.setBackground(Color.PINK);
				zipField.setToolTipText("Field cannot be empty");
			} else if (!checkIsDigit(zipField.getText())
					|| zipField.getText().length() != 5) {
				numberError++;
				zipField.setBackground(Color.PINK);
				zipField.setToolTipText("Zip Code must contain only digits 0-9 and must be five digits long");
			} else {
				resetErrorField(zipField);
			}

			if (schoolIDField.getText().length() == 0) {
				numberError++;
				schoolIDField.setBackground(Color.PINK);
				schoolIDField.setToolTipText("Field cannot be empty");
			} else {
				resetErrorField(schoolIDField);
			}
			if (organizationField.getText().length() == 0
					|| (!checkIsDigit(locField.getText()) && !checkIsCharacter(locField
							.getText()))) {
				numberError++;
				organizationField.setBackground(Color.PINK);
				organizationField
						.setToolTipText("Organization Field must contain only letter A-Z");
			} else {
				resetErrorField(organizationField);
			}
			if (locField.getText().length() == 0
					|| (!checkIsDigit(locField.getText()) && !checkIsCharacter(locField
							.getText()))) {
				numberError++;
				locField.setBackground(Color.PINK);
				locField.setToolTipText("Location ID must contain only digits 0-9");
			} else {
				resetErrorField(locField);
			}

			if (roomField.getText().length() == 0
					|| (!checkIsDigit(roomField.getText()) && !checkIsCharacter(roomField
							.getText()))) {
				numberError++;
				roomField.setBackground(Color.PINK);
				roomField
						.setToolTipText("Room must contain only digits 0-9 and letter A-Z");
			} else {
				resetErrorField(roomField);
			}

			if (ageField.getText().length() == 0
					|| !checkIsDigit(ageField.getText())) {
				numberError++;
				ageField.setBackground(Color.PINK);
				ageField.setToolTipText("Age must contain only digits 0-9");
			} else {
				int difference = Integer.parseInt(dateField.getText()
						.substring(6, 10))
						- Integer.parseInt(birthdayField.getText().substring(6,
								10));
				if (Integer.parseInt(ageField.getText()) == difference
						|| Integer.parseInt(ageField.getText()) == difference - 1) {
					resetErrorField(ageField);
				} else {
					numberError++;
					ageField.setBackground(Color.PINK);
					ageField.setToolTipText("Age does not match with the birthday");
				}
			}

			if (genderField.getText().length() == 0
					|| !checkGender(genderField.getText())) {
				numberError++;
				genderField.setBackground(Color.PINK);
				genderField
						.setToolTipText("Gender must contain only 'M' or 'F'");
			} else {
				genderField.setText(gender);
				resetErrorField(genderField);
			}

			if (ethnicityField.getText().length() == 0
					|| !checkEthnicity(ethnicityField.getText())) {
				numberError++;
				ethnicityField.setBackground(Color.PINK);
				ethnicityField
						.setToolTipText("Ethnicity must contain only 'A', 'C', 'H' 'AA', 'EI', 'ME', 'NA', or 'PI'");
			} else {
				ethnicityField.setText(eth);
				resetErrorField(ethnicityField);
			}

			if (schoolYearField.getText().length() == 0
					|| !checkIsDigit(schoolYearField.getText())) {
				numberError++;
				schoolYearField.setBackground(Color.PINK);
				schoolYearField
						.setToolTipText("School year must contain only digits 0-9");
			} else {
				resetErrorField(schoolYearField);
			}

			if (languageField.getText().length() == 0
					|| !checkLanguage(languageField.getText())) {
				numberError++;
				languageField.setBackground(Color.PINK);
				languageField
						.setToolTipText("Language must contain only 'E', 'S', or 'O'");
			} else {
				languageField.setText(language);
				resetErrorField(languageField);
			}

			if (checkComment(commentField.getText())) {
				commentField.setText(comment);
				resetErrorField(languageField);
			}

			if (numberError == 0) {
				WriteToExcel test = new WriteToExcel(fullname,
						receiptNumField.getText(), lastNameField.getText()
								+ "," + " " + firstNameField.getText(),
						dateField.getText(), locationField.getText(),
						screenerField.getText(), birthdayField.getText(),
						regionField.getText(), zipField.getText(),
						schoolIDField.getText(), organizationField.getText(),
						locField.getText(), ageField.getText(),
						genderField.getText(), ethnicityField.getText(),
						roomField.getText(), schoolYearField.getText(),
						languageField.getText(), commentField.getText(),
						textFile);
				try {
					// Not deleting those fields prevents screeners to enter
					// same data
					test.write();
					receiptNumField.setText(Integer.toString(Integer
							.parseInt(receiptNumField.getText()) + 1));
					firstNameField.setText("");
					lastNameField.setText("");
					// dateField.setText("");
					// locationField.setText("");
					// screenerField.setText("");
					birthdayField.setText("");
					// regionField.setText("");
					// zipField.setText("");
					schoolIDField.setText("");
					// organizationField.setText("");
					// locField.setText("");
					ageField.setText("");
					genderField.setText("");
					ethnicityField.setText("");
					// roomField.setText("");
					// schoolYearField.setText("");
					languageField.setText("");
					// commentField.setText("");
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
			numberError = 0;
		}
	}

	@SuppressWarnings("serial")
	private final class finishAction extends AbstractAction {
		private finishAction() {
			super("Finish", new ImageIcon("exit.png"));
		}

		public void actionPerformed(ActionEvent e) {
			StringBuffer message = new StringBuffer();
			message.append(receiptNumField.getText() + "\n"
					+ firstNameField.getText() + "\n" + lastNameField.getText()
					+ "\n" + dateField.getText() + "\n"
					+ locationField.getText() + "\n" + screenerField.getText()
					+ "\n" + birthdayField.getText() + "\n"
					+ ageField.getText() + "\n" + genderField.getText() + "\n"
					+ ethnicityField.getText() + "\n" + regionField.getText()
					+ "\n" + zipField.getText() + "\n"
					+ schoolIDField.getText() + "\n"
					+ schoolYearField.getText() + "\n"
					+ organizationField.getText() + "\n"
					+ languageField.getText() + "\n" + locationField.getText());
			System.exit(0);
		}
	}

	// @SuppressWarnings("serial")
	/*
	 * private final class replaceAction extends AbstractAction { private
	 * replaceAction() { super("Replace", new ImageIcon("replace.png")); }
	 * 
	 * public void actionPerformed(ActionEvent e) {
	 * JOptionPane.showMessageDialog(null,
	 * "Replace funtion is currently disabled"); if
	 * (receiptNumField.getText().length() == 0 ||
	 * !checkIsDigit(receiptNumField.getText())) {
	 * receiptNumField.setBackground(Color.PINK);
	 * receiptNumField.setToolTipText(
	 * "Please enter the receipt number you want to replace"); } else if
	 * (replaceField.getText().length() == 0 ||
	 * !checkIsDigit(replaceField.getText())) {
	 * replaceField.setBackground(Color.PINK); replaceField
	 * .setToolTipText("Please enter the receipt number you want to replace with"
	 * ); } else { resetErrorField(replaceField);
	 * resetErrorField(receiptNumField); if (leftButton.isSelected()){ } else {
	 * 
	 * } } } }
	 */

	@SuppressWarnings("serial")
	private final class deleteAction extends AbstractAction {
		private deleteAction() {
			super("Delete", new ImageIcon("delete.png"));
		}

		public void actionPerformed(ActionEvent e) {
			if (receiptNumField.getText().length() == 0
					|| !checkIsDigit(receiptNumField.getText())) {
				receiptNumField.setBackground(Color.PINK);
				receiptNumField
						.setToolTipText("Please enter the receipt number you want to delete");
			} else {
				String message = "Are you sure you want to delete this entry?";
				String title = "Confirm Delete";
				int reply = JOptionPane.showConfirmDialog(null, message, title,
						JOptionPane.YES_NO_OPTION);
				if (reply == JOptionPane.YES_OPTION) {
					resetErrorField(receiptNumField);
					firstNameField.setText("");
					lastNameField.setText("");
					dateField.setText("");
					locationField.setText("");
					screenerField.setText("");
					birthdayField.setText("");
					regionField.setText("");
					zipField.setText("");
					schoolIDField.setText("");
					organizationField.setText("");
					locField.setText("");
					ageField.setText("");
					genderField.setText("");
					ethnicityField.setText("");
					roomField.setText("");
					schoolYearField.setText("");
					languageField.setText("");
					commentField.setText("");
					WriteToExcel test = new WriteToExcel(fullname,
							receiptNumField.getText(), lastNameField.getText()
									+ "," + " " + firstNameField.getText(),
							dateField.getText(), locationField.getText(),
							screenerField.getText(), birthdayField.getText(),
							regionField.getText(), zipField.getText(),
							schoolIDField.getText(),
							organizationField.getText(), locField.getText(),
							ageField.getText(), genderField.getText(),
							ethnicityField.getText(), roomField.getText(),
							schoolYearField.getText(), languageField.getText(),
							commentField.getText(), "", "", "", "", "", "", "",
							"", "", "");
					try {
						test.delete();
						receiptNumField.setText("");
						firstNameField.setText("");
						lastNameField.setText("");
						dateField.setText("");
						locationField.setText("");
						screenerField.setText("");
						birthdayField.setText("");
						regionField.setText("");
						zipField.setText("");
						schoolIDField.setText("");
						organizationField.setText("");
						locField.setText("");
						ageField.setText("");
						genderField.setText("");
						ethnicityField.setText("");
						roomField.setText("");
						schoolYearField.setText("");
						languageField.setText("");
						commentField.setText("");
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		}
	}

	@SuppressWarnings("serial")
	private final class cancelAction extends AbstractAction {

		private cancelAction() {
			super("Clear", new ImageIcon("clear.png"));
		}

		public void actionPerformed(ActionEvent e) {
			receiptNumField.setText("");
			firstNameField.setText("");
			lastNameField.setText("");
			dateField.setText("");
			locationField.setText("");
			screenerField.setText("");
			birthdayField.setText("");
			regionField.setText("");
			zipField.setText("");
			schoolIDField.setText("");
			organizationField.setText("");
			locField.setText("");
			ageField.setText("");
			genderField.setText("");
			ethnicityField.setText("");
			roomField.setText("");
			schoolYearField.setText("");
			languageField.setText("");
			commentField.setText("");
			resetErrorField(receiptNumField);
			resetErrorField(firstNameField);
			resetErrorField(lastNameField);
			resetErrorField(dateField);
			resetErrorField(locationField);
			resetErrorField(screenerField);
			resetErrorField(birthdayField);
			resetErrorField(regionField);
			resetErrorField(zipField);
			resetErrorField(schoolIDField);
			resetErrorField(ageField);
			resetErrorField(genderField);
			resetErrorField(ethnicityField);
			resetErrorField(roomField);
			resetErrorField(schoolYearField);
			resetErrorField(languageField);
			resetErrorField(organizationField);
			resetErrorField(locField);
		}
	}
}
